home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / NextAnswers / 1599_niread.c < prev    next >
Text File  |  1995-11-28  |  2KB  |  90 lines

  1. /*
  2.  * niread
  3.  *
  4.  * usage: niread domain directory
  5.  * compile with: cc -o niread niread.c
  6.  *
  7.  * Similar to "niutil -read domain directory"
  8.  *
  9.  * Sample output:
  10.  *
  11.  *    myhost> niread . /users/me
  12.  *    "name": "me"
  13.  *    "passwd": "BBmuFjTkKx/f."
  14.  *    "uid": "20"
  15.  *    "gid": "20"
  16.  *    "realname": "My Account"
  17.  *    "home": "/me"
  18.  *    "shell": "/bin/csh"
  19.  *    "_writers_passwd": "me"
  20.  *
  21.  * Author: Marc Majka
  22.  * You may freely copy, distribute and reuse the code in this example.  
  23.  * NeXT disclaims any warranty of any kind, expressed or implied, as to 
  24.  * its fitness for any particular use.
  25.  */
  26.  
  27. #import <ansi/stdio.h>
  28. #import <bsd/errno.h>
  29. #import <netinfo/ni.h>
  30.  
  31. main(int argc, char *argv[]) {
  32.  
  33.     int         pn, vn;
  34.     ni_proplist plist;
  35.     ni_property prop;
  36.     ni_namelist values;
  37.     void        *dom;
  38.     ni_id       dir;    
  39.     ni_status   ret;
  40.  
  41.     /* check usage */
  42.     if (argc < 3) {
  43.         fprintf(stderr, "usage: %s domain directory\n", argv[0]);
  44.         exit(1);
  45.     }
  46.  
  47.     /* argv[1] should be a domain name */
  48.     ret = ni_open(NULL, argv[1], &dom);
  49.     if (ret != NI_OK) {
  50.         fprintf(stderr, "ni_open: %s\n", ni_error(ret));
  51.         exit(1);
  52.     }
  53.  
  54.     /* argv[2] should be a directory specification */
  55.     ret = ni_pathsearch(dom, &dir, argv[2]);
  56.     if (ret != NI_OK) {
  57.         fprintf(stderr, "ni_pathsearch: %s\n", ni_error(ret));
  58.         exit(1);
  59.     }
  60.  
  61.     /* get the property list stored in the this directory */
  62.     ret = ni_read(dom, &dir, &plist);
  63.     if (ret != NI_OK) {
  64.         fprintf(stderr, "ni_read: %s\n", ni_error(ret));
  65.         exit(1);
  66.     }
  67.  
  68.     /* for each property */
  69.     for (pn = 0; pn <  plist.ni_proplist_len; pn++) {
  70.  
  71.         prop = plist.ni_proplist_val[pn];
  72.  
  73.         /* print the property key enclosed in quotes */
  74.         printf("\"%s\":", prop.nip_name);
  75.     
  76.         values = prop.nip_val;
  77.  
  78.         /* for each value in the namelist for this property */
  79.         for (vn = 0; vn < values.ni_namelist_len; vn++) {
  80.             /* print the value enclosed in quotes */
  81.             printf(" \"%s\"", values.ni_namelist_val[vn]);
  82.         }
  83.         printf("\n");
  84.     }
  85.  
  86.     /* clean up */
  87.     ni_free(dom);
  88.  
  89.     exit(0);
  90. }